package com.pdstudio.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.bean.ArticleDetail;
import com.pdstudio.bean.ArticleSearchList;
import com.pdstudio.bean.LiveImg;
import com.pdstudio.bean.LiveIndex;
import com.pdstudio.bean.ResponseData;
import com.pdstudio.bean.WeatherImg;

import android.content.Context;

public class LiveHttpMethod extends HttpMethodBase {

	private String funcName = "live";
	private String NEWSLIST = "getsciencelist";
	private String NEWSDETAIL = "getsciencedetail";
	private String PICPRAISE = "praise";// 图片点赞
	private String PICLIST = "getliveimglist";
	private String EMERGENCY = "getemergency";
	private String GETLIVEINDEX = "getliveindex";

	public LiveHttpMethod(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 获取科普文章列表
	 * */
	public List<ArticleSearchList> GetArticleList(String appKey, String typeId) {
		List<ArticleSearchList> result = new ArrayList<ArticleSearchList>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("typeid", "1");
			String uri = buildUri(funcName, NEWSLIST, appKey, parameters);
			System.out.println(uri);
			ResponseData<List<ArticleSearchList>> response = new Gson()
					.fromJson(
							connGetServerForResult(uri),
							new TypeToken<ResponseData<List<ArticleSearchList>>>() {
							}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取生活指数列表
	 * */
	
	public List<LiveIndex> GetLiveIndexList(String appKey,String address){
		List<LiveIndex> result  = new ArrayList<LiveIndex>();
		try{
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", address);
			String uri = buildUri(funcName, GETLIVEINDEX, appKey, parameters);
			ResponseData<List<LiveIndex>> response = new Gson()
					.fromJson(
							connGetServerForResult(uri),
							new TypeToken<ResponseData<List<LiveIndex>>>() {
							}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		}
		catch (Exception e){
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * 获取科普文章详细信息
	 * */
	public ArticleDetail GetEmergency(String appKey, String addressCode) {
		ArticleDetail result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, EMERGENCY, appKey, parameters);
			System.out.println(uri);
			ResponseData<ArticleDetail> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<ArticleDetail>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取应急响应
	 * */
	public ArticleDetail GetArticleDetail(String appKey, String articleId) {
		ArticleDetail result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("articleId", "1");
			String uri = buildUri(funcName, NEWSDETAIL, appKey, parameters);
			System.out.println(uri);
			ResponseData<ArticleDetail> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<ArticleDetail>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 图片点赞
	 */
	public ResponseData TakePicPraise(String appKey, int picid) {
		ResponseData result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("id", picid + "");
			String uri = buildUri(funcName, PICPRAISE, appKey, parameters);
			System.out.println(uri);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair nv = new BasicNameValuePair("id", picid + "");
			params.add(nv);
			ResponseData response = new Gson().fromJson(
					connPostServerForResult(uri, params),
					new TypeToken<ResponseData>() {
					}.getType());
			result = response;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取实景图片
	 */
	public List<LiveImg> GetLiveImgs(String appKey, String timestamp,
			String address) {
		List<LiveImg> result = new ArrayList<LiveImg>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("timestamp", timestamp);
			parameters.put("address", address);
			String uri = buildUri(funcName, PICLIST, appKey, parameters);
			System.out.println(uri);
			ResponseData<List<LiveImg>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<LiveImg>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
